package com.hfengxiang.example.canvasrotatedemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class MatrixView1 extends View implements RotateGestureDetector.OnRotateGestureListener {

    private Matrix mMatrix;
    private Paint paint;
    private Bitmap bitmap;
    private float currentScale;
    private RotateProcessor rotateProcessor;
    private float lastTotal;
    private RotateGestureDetector mRotateGestureDetector;

    public MatrixView1(Context context) {
        this(context, null);
    }

    public MatrixView1(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public MatrixView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, -1);
    }

    public MatrixView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        bitmap = Utils.getPhoto(getResources(), getResources().getDisplayMetrics().widthPixels);
        mMatrix = new Matrix();
        mRotateGestureDetector = new RotateGestureDetector(getContext(),this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(getWidth() / 2f, getHeight() / 2f);
        canvas.drawBitmap(bitmap, mMatrix, paint);
        Utils.drawCoordinate(canvas,0,0,false);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mRotateGestureDetector.onTouchEvent(event);
        return true;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        currentScale = ScaleCalculator.calculateScale(getWidth(), getHeight(), bitmap.getWidth(), bitmap.getHeight(), 0);
        mMatrix.postTranslate(-bitmap.getWidth() / 2f, -bitmap.getHeight() / 2f);
        mMatrix.postScale(currentScale, currentScale);
    }


    /**
     * 根据旋转的角度计算出图片复位应当到达的角度
     *
     * @param degree 旋转的角度
     * @return 目标角度
     */
    private int computeTargetDegree(double degree) {
        int[] degrees = {0, 90, 90, 180, 180, 270, 270, 360};
        degree = degree % 360;
        int index = (int) (degree / 45);
        if (index > 0) {
            return degrees[index];
        } else {
            return -1 * degrees[-1 * index];
        }
    }

    @Override
    public void onRotate(int degrees, int pivotX, int pivotY) {
        Log.i("onRotate","pivotX="+pivotX);
        Log.i("onRotate","pivotY="+pivotY);
        mMatrix.postRotate(degrees,pivotX- getWidth() / 2f,pivotY-getHeight()/2f);
        invalidate();
    }

    @Override
    public void onToRightAngle(int pivotX, int pivotY) {

    }
}
